<?php
namespace control;
/*
*就餐记录
*/


class mealRecordClass extends adIndexClass
{
    private const page = 'mealRecord';
    private const table = 'meal_record';
    private const title = '就餐记录';
    public array $mealType = ['早餐', '中餐', '晚餐'];

    /**
     * 查询条件
     *
     * @param string $workFlow
     * @return string
     */
    public function home($workFlow = '就餐记录'): string
    {
        if ($workFlow == '就餐记录') {
            $data = [
                "id" => "all",
                "menu" => [
                    "record" => "就餐记录",
                    "peopleStatistics" => "按人汇总",
                    "mealStatistics" => "按餐汇总",
                ]
            ];
        }
        $data['html'] = $this->homeForm($workFlow);
        return $this->tab($data);
    }

    /**
     * 查询表单
     *
     * @param string $workFlow
     * @return string
     */
    public function homeForm(string $workFlow): string
    {
        $html='';
        if ($workFlow == '就餐记录') {
            $html =
                select(["name" => "canteen", 'class' => 'text', 'title' => '食堂名称', 'option' => explode('，', para('canteenOption'))]) .
                select(["name" => "type", 'title' => '餐别', 'option' => $this->mealType]) .
                text(array("name" => "creatTime", 'laydate' => ['type' => 'date', 'range' => '~'], "title" => "日期"));
        } elseif ($workFlow == '按人汇总') {
            $html =selectMultiple(["name" => "stid", 'table' => " staff", 'sqlId' => 'stid', 'sqlField' => 'name', 'title' => '员工']) .
                selectMultiple(["name" => "departmentId", 'table' => " department", 'sqlId' => 'id', 'sqlField' => 'name', 'title' => '部门']).
            select(["name" => "canteen", 'class' => 'text', 'title' => '食堂名称', 'option' => explode('，', para('canteenOption'))]) .
            select(["name" => "type", 'title' => '餐别', 'option' => $this->mealType]) .
            text(array("name" => "creatTime", 'laydate' => ['type' => 'date', 'range' => '~'], "title" => "日期"));
        } elseif ($workFlow == '按餐汇总') {
            $html =selectMultiple(["name" => "stid", 'table' => " staff", 'sqlId' => 'stid', 'sqlField' => 'name', 'title' => '员工']) .
                selectMultiple(["name" => "departmentId", 'table' => " department", 'sqlId' => 'id', 'sqlField' => 'name', 'title' => '部门']).
                select(["name" => "canteen", 'class' => 'text', 'title' => '食堂名称', 'option' => explode('，', para('canteenOption'))]) .
                select(["name" => "type", 'title' => '餐别', 'option' => $this->mealType]) .
                text(array("name" => "creatTime", 'laydate' => ['type' => 'date', 'range' => '~'], "title" => "日期"));
        }

        return $this->search($html, 'menu');
    }

    /**
     * 按人汇总
     * @return bool|string
     */
    public function peopleStatistics(){
        $json['html'] = $this->homeForm('按人汇总');
        return json_encode($json, 320);
    }

    public function peopleStatisticsSearch(){
        //like查询数组
        $like = [];
        //全等查询数组
        $equal = array(
            "b.stid" => $this->post('stid'),//员工
            "a.canteen" => $this->post('canteen'),//食堂名称
            "a.type" => $this->post('type'),//类型
        );
        $and = "";
        if (!empty($this->post('departmentId'))){
            //部门
            $and.=" and b.stid in (select stid from staff where jobId in (select id from job where departmentId = '{$this->post('departmentId')}'))";
        }
        if ($this->post('creatTime')) {
            $timeArr = explode('~', $this->post('creatTime'));
            $and .= " and a.recordDate >='{$timeArr[0]}' and a.recordDate <='{$timeArr[1]}'";
        }
        //获取查询结果
        $where = '1=1'.search(array("like" => $like, "=" => $equal)) . $and." group by b.stid";
        $page = $this->page(array("table" => self::table." as a join `meal_record_detail` as b on a.id = b.orderId", 'field'=>"any_value(b.stid) as stid,any_value(a.canteen) as canteen,any_value(a.type) as type,any_value(count(b.id)) as total","where" => $where));
        $_SESSION[self::page]['peopleSql'] = $page['fullSql'];
        //获取表头
        $tr[] = array("部门", "员工", "食堂名称", '餐别', '就餐次数');
        //获取记录
        foreach ($page['array'] as $array) {
            $staff = query('staff'," stid = '$array[stid]'");
            $job = query('job'," id = '$staff[jobId]'");
            $department = query('department'," id = '$job[departmentId]'");
            $tr[] = array($department['name'], $staff['name'], $array['canteen'], $array['type'],  $array['total']);
        }
        $button='';
        if ($this->power(self::page, "export")) {
            $button .= "  <a href='" . root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=exportPeopleStatistics'><span class='spanButton'>导出</span></a>\n";
        }
        //返回
        $json['html'] = $page['hint'] . $button . "<form name='peopleStatisticsForm'>" . space . $this->tableMany(array("tr" => $tr)) . "</form>" . $page['button'] . clear;
        return json_encode($json, 320);
    }

    /**
     * 按餐汇总
     * @return bool|string
     */
    public function mealStatistics(){
        $json['html'] = $this->homeForm('按人汇总');
        return json_encode($json, 320);
    }

    public function mealStatisticsSearch(){
        //like查询数组
        $like = [];
        //全等查询数组
        $equal = array(
            "b.stid" => $this->post('stid'),//员工
            "a.canteen" => $this->post('canteen'),//食堂名称
            "a.type" => $this->post('type'),//类型
        );
        $and = "";
        if (!empty($this->post('departmentId'))){
            //部门
            $and.=" and b.stid in (select stid from staff where jobId in (select id from job where departmentId = '{$this->post('departmentId')}'))";
        }
        if ($this->post('creatTime')) {
            $timeArr = explode('~', $this->post('creatTime'));
            $and .= " and a.recordDate >='{$timeArr[0]}' and a.recordDate <='{$timeArr[1]}'";
        }
        //获取查询结果
        $where = '1=1'.search(array("like" => $like, "=" => $equal)) . $and." group by a.canteen,a.type";

        $page = $this->page(array("table" => self::table." as a join meal_record_detail as b on a.id = b.orderId", 'field'=>"a.canteen,a.type,count(b.id) as total,sum(a.visitorNum) as visitorNum","where" => $where));

        $_SESSION[self::page]['mealStatistics'] = $page['fullSql'];
        $tr[] = array("食堂名称", '餐别', '员工就餐人数','访客就餐人数','合计人数');
        //获取记录
        foreach ($page['array'] as $array) {
            $visitorNum = find(array('table'=>self::table,'where'=>"canteen = '$array[canteen]' and type = '$array[type]'",'sum'=>"visitorNum"));
            $tr[] = array($array['canteen'], $array['type'],  $array['total'],$visitorNum,$array['total']);
        }
        $button='';
        if ($this->power(self::page, "export")) {
            $button .= "  <a href='" . root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=exportMealStatistics'><span class='spanButton'>导出</span></a>\n";
        }
        //返回
        $json['html'] = $page['hint'] . $button . "<form name='mealStatisticsForm'>" . space . $this->tableMany(array("tr" => $tr)) . "</form>" . $page['button'] . clear;
        return json_encode($json, 320);
    }


    /*客户列表-查询结果*/
    public function homeSearch()
    {
        //like查询数组
        $like = [];
        //全等查询数组
        $equal = array(
            "canteen" => $this->post('canteen'),//食堂名称
            "type" => $this->post('type'),//类型
        );
        $and = "";
        if ($this->post('creatTime')) {
            $timeArr = explode('~', $this->post('creatTime'));
            $and .= " and recordDate >='{$timeArr[0]}' and recordDate <='{$timeArr[1]}'";
        }
        //获取查询结果
        $where = search(array("like" => $like, "=" => $equal)) . $and;

        $page = $this->page(array("table" => self::table, "where" => $where, "order" => " recordDate desc,time desc", ));
        $_SESSION[self::page]['sql'] = $page['fullSql'];
        //获取表头
        $checkAll = $this->checkAll("listForm", "listId");
        $tr[] = array($checkAll, "日期", "食堂名称", "餐别", '员工就餐人数', '访客就餐人数', '创建人', '更新时间');
        //获取记录
        $url = root . "control/adIndex.html?cla=" . self::page;
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
                "detail" => array("title" => self::title . "详情", "click" => "newIframe('" . self::title . "详情','" . $url . "&fun=detail&id=" . $array['id'] . "')"),
            );
            $staffNum = static::total(array('table' => 'meal_record_detail', 'where' => " and orderId = '$array[id]'"));

            $tr[] = array($this->dominate($dominate), $array['recordDate'], $array['canteen'], $array['type'], $staffNum, $array['visitorNum'], self::findStaff($array['stid']), $array['updateTime']);
        }
        //控件
        $button = "";
        if ($this->power(self::page, "edit")) {
            $button .= "<span onclick=\layer('".self::page."','edit')\" class='spanButton'>新增</span>\n";
        }
        if ($this->power(self::page, "del")) {
            $button .= "<span class='spanButton' onclick=\"layer('{$this->get('cla')}', 'operate', {'act': 'del'})\">删除</span>\n";
        }
        if ($this->power(self::page, "edit")) {
            $button .=
                "<a href='" . root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=downloadModel'><span class='spanButton'>导入模板</span></a>" .
                $this->upButton(array("title" => "导入", "fun" => "importData"));
        }
        if ($this->power(self::page, "edit")) {
            $button .= "  <a href='" . root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=exportData'><span class='spanButton'>导出</span></a>\n";
        }
        //返回
        $json['html'] = $page['hint'] . $button . "<form name='listForm'>" . space . $this->tableMany(array("tr" => $tr)) . "</form>" . $page['button'] . clear;
        return json_encode($json, 320);
    }


    /**
     * 下载模板
     */
    public function downloadModel()
    {
        if (!$this->power(self::page, 'edit')) {
            $_SESSION['warn'] = '你还没有编辑的权限';
        } else {
            $title = ["日期", "食堂", '餐别(早餐，午餐，晚餐)', '员工就餐人员','员工就餐人员编号(多个请用英文逗号隔开)', '访客就餐人数', '备注'];
            $data[] = ['2021-01-01','默认食堂','早餐','张三,李四','YM20210001,YM20210002','10','样例数据，导入时请删除本条数据'];
            $text = $this->control['name'] . "下载了就餐记录导入模板";
            $this->logText(["type" => self::page, "text" => $text]);
            $this->excelOut($title, $data, '就餐记录导入模板.xlsx');
            exit(0);
        }
        header("Location:" . getenv("HTTP_REFERER"));
    }

    /*数据导入分析*/
    public function importData()
    {
        if (!$this->power(self::page, 'edit')) {
            $_SESSION['warn'] = '你还没有编辑的权限';
        } else {
             $fomatArray = [];//导入数据格式同一为TYPE_STRING
            foreach ($this->importFieldInit() as $value){
                $fomatArray[] = \Vtiful\Kernel\Excel::TYPE_STRING;
            }
            $data = $this->importHead(format: $fomatArray);
            $x = 0;
            $text = '';
            foreach ($data as $i => $row) {
                if ($i > 0) {
                    $cell = formSubArray($row);

                    $field = array(
                        "id" => self::getId(),
                        'stid' => $this->control['stid'],
                        'recordDate' => $cell[0],
                        'canteen' => $cell[1],
                        'type' => $cell[2],
                        'visitorNum' => $cell[5],
                        'text' => $cell[6],
                        'updateTime' => $this->time,
                        "time" => date("Y-m-d H:i:s"),
                    );
                    $userNo = codeTemplateClass::generateCode(self::page,[
                        'prefix' => 'YRSQ' . date('Ymd'),
                        'table' => self::table,
                        'field' => 'orderNo',
                        'where' => '',
                    ],$field);
                    $field['orderNo'] = $userNo;

                    $annotation = [
                        'recordDate' => '日期',
                        'canteen' => '食堂名称',
                        'type' => '餐别',
                        'member' => '员工就餐人员',
                    ];
                    $rules = [
                        'recordDate' => 'required',
                        'canteen' => 'required',
                        'type' => 'required',
                        'member' => 'required',
                    ];
                    // 校验是否符合规则
                    $vt = new \verify();
                    $result = $vt->setAliases($annotation)->validate($field, $rules)->isOk();
                    if ($result === false) {
                        $text .= "第【{$i}】行：" . $vt->getErrs()[0] . "<br>";  // 多个规则提示最前面一条
                    }elseif(!in_array($field['type'],$this->mealType)){
                        $text .= "第【{$i}】行：导入失败，餐别类型错误<br>";  // 多个规则提示最前面一条
                    } else {
                        $warn = insert(["table" => self::table, "field" => $field]);
                        if (in_array($warn, ["新增成功", "更新成功"])) {
                            //分割员工就餐人员，找到对应ID
                            $staffArr = explode(',', $cell[4]);
                            if (is_array($staffArr)) {
                                foreach ($staffArr as $val) {
                                    $staff = query('staff', " orderNo = '$val'");
                                    if (!empty($staff['stid'])) {
                                        insert(array(
                                            'table' => 'meal_record_detail',
                                            'field' => array(
                                                'id' => self::getId(),
                                                'orderId' => $field['id'],
                                                'stid' => $staff['stid'],
                                                'time' => $this->time
                                            )
                                        ));
                                    }
                                }
                            }
                            $x++;
                            $text .= "第【{$i}】行：导入成功<br>";
                        } else {
                            $text .= "第【{$i}】行：导入失败：编辑失败<br>";
                        }
                    }
                }
            }
            $text = $this->control['name'] . '导入了文件：' . $_FILES['file']['name'] . '：' . $text;
            $this->logText(["type" => self::page, "text" => $text]);
            $warn = "成功导入" . $x . "条数据，详情请查看日志！";
            $_SESSION['warn'] = $warn;
        }
        header("Location:" . getenv("HTTP_REFERER"));
        exit(0);
    }

    /**
     * 导出
     */
    public function exportData()
    {
        if (!$this->power(self::page, 'export')) {
            $_SESSION['warn'] = '你还没有导出的权限';
        } else {
            $obj = new performanceScoreClass();
            $data = [];
            $sql = $_SESSION[self::page]['sql'];

            $title = ["编号", "日期", "食堂名称", '餐别', '员工就餐人员', '访客就餐人数', '备注', '创建人', '创建时间'];
            $result = self::sqlFind($sql);
            if (is_array($result)) {
                foreach ($result as $array) {
                    $member = find(array('table'=>self::table.'Mx','single'=>" orderId = '$array[id]' group by orderId",'field'=>" group_concat(stid) as member"));
                    $data[] = [$array['orderNo'], $array['recordDate'], $array['type'], $array['canteen'], $obj->findMember($member['member'], 2), $array['text'], self::findStaff($array['stid']), $array['time']];
                }
            }
            $text = $this->control['name'] . "导出了" . self::title;
            $this->logText(["type" => self::page, "text" => $text]);
            $this->excelOut($title, $data, self::title . ".xlsx");
        }
        header("Location:" . getenv("HTTP_REFERER"));
        exit(0);
    }

    /**
     * 按人汇总导出
     */
    #[NoReturn] public function exportPeopleStatistics()
    {
        if (!$this->power(self::page, 'export')) {
            $_SESSION['warn'] = '你还没有导出的权限';
        } else {
            $data = [];
            $sql = $_SESSION[self::page]['peopleSql'];

            $title = ["部门", "员工", "食堂名称", '餐别', '就餐次数'];
            $result = self::sqlFind($sql);
            if (is_array($result)) {
                foreach ($result as $array) {
                    $staff = query('staff'," stid = '$array[stid]'");
                    $job = query('job'," id = '$staff[jobId]'");
                    $department = query('department'," id = '$job[departmentId]'");
                    $data[] = array($department['name'], $staff['name'], $array['canteen'], $array['type'],  $array['total']);
                }
            }
            $text = $this->control['name'] . "导出了按人汇总的就餐记录";
            $this->logText(["type" => self::page, "text" => $text]);
            $this->excelOut($title, $data, "按人汇总的就餐记录.xlsx");
        }
        header("Location:" . getenv("HTTP_REFERER"));
        exit(0);
    }

    /**
     * 按餐汇总导出
     */
    public function exportMealStatistics()
    {
        if (!$this->power(self::page, 'export')) {
            $_SESSION['warn'] = '你还没有导出的权限';
        } else {
            $data = [];
            $sql = $_SESSION[self::page]['mealStatistics'];

            $title =array("食堂名称", '餐别', '员工就餐人数','访客就餐人数','合计人数');
            $result = self::sqlFind($sql);
            if (is_array($result)) {
                foreach ($result as $array) {
                    $visitorNum = find(array('table'=>self::table,'where'=>"canteen = '$array[canteen]' and type = '$array[type]'",'sum'=>"visitorNum"));
                    $data[] =array($array['canteen'], $array['type'],  $array['total'],$visitorNum,$array['total']);
                }
            }
            $text = $this->control['name'] . "导出了按餐汇总的就餐记录";
            $this->logText(["type" => self::page, "text" => $text]);
            $this->excelOut($title, $data, "按餐汇总的就餐记录.xlsx");
        }
        header("Location:" . getenv("HTTP_REFERER"));
        exit(0);
    }


    /**
     * 编辑页
     * @return string
     */
    public function edit()
    {
        $id = $this->post('id')?:self::getId();
        $info = query(self::table, " id = '" . $id . "' ");
        //表单
        $html = $this->layerEdit([
            "table" => [
                [
                    must . "日期：" => text(["name" => "recordDate", 'laydate' => [], 'class' => 'text', "value" => $info['recordDate']]),
                    must . "食堂名称：" => select(["name" => "canteen", 'class' => 'text', 'title' => '食堂名称', 'option' => explode('，', para('canteenOption')), "value" => $info['canteen']]),
                ],
                [
                    must . "餐别：" => radio(["name" => "type", 'value' => $this->mealType, "checked" => $info['type']]),
                    must . "员工就餐人员：" => componentClass::staffSelector($info['selector'])
                ],
                [
                    "访客就餐人数：" => text(["name" => "visitorNum", 'class' => 'text', "value" => $info['visitorNum']]),
                ],
                [
                    "备注：" => textarea(["name" => "text", 'class' => 'textarea', "value" => $info['text']]),
                ],
            ],
            'hidden' => hidden(['name' => 'id', 'value' => $id]),
        ]);
        //返回
        return $this->layer(array(
            "title" => L('job.button.editor').self::title,
            "height" => 350,
            "html" => $html,
            'button' => '<button class="button" onclick="subForm(\'' . self::page . 'EditForm\',root+\'control/adIndex.html?cla=' . self::page . '&fun=editEdit\')">保存</button>',
        ));
    }

    public function editEdit()
    {
        $id = $this->post('id') ?: self::getId();
        $field = [
            'recordDate' => $this->post('recordDate'),
            'canteen' => $this->post('canteen'),
            'type' => $this->post('type'),
            'visitorNum' => $this->post('visitorNum'),
            'selector'       => $this->post('staff')[0]??'',//员工选择器
            'text' => $this->post('text'),
            'updateTime' => $this->time
        ];
        $annotation = [
            'recordDate' => '日期',
            'canteen' => '食堂名称',
            'type' => '餐别',
        ];
        $rules = [
            'recordDate' => 'required',
            'canteen' => 'required',
            'type' => 'required',
        ];
        $staffIdArr = componentClass::returnStaffId($field['selector']);
        // 校验是否符合规则
        $vt = new \verify();
        $result = $vt->setAliases($annotation)->validate($field, $rules)->isOk();
        if ($result === false) {
            $json['warn'] = $vt->getErrs()[0];  // 多个规则提示最前面一条
            goto resp;
        }
        //判断
        if (!$this->power(self::page, "edit")) {
            $json['warn'] = "你没有编辑" . self::title . "的权限";
        } elseif (empty($staffIdArr)) {
            $json['warn'] = '请选择员工就餐人员';
        } else {
            $where = " id = '$id' ";
            $info = query(self::table, $where);
            if (!$info['id']) {
                $userNo = codeTemplateClass::generateCode(self::page,[
                    'prefix' => 'YRSQ' . date('Ymd'),
                    'table' => self::table,
                    'field' => 'orderNo',
                    'where' => '',
                ],$field);
                $field = array_merge($field, [
                    'id' => $id,
                    'orderNo' => $userNo,
                    'stid' => $this->control['stid'],
                    'time' => $this->time
                ]);
                $json['warn'] = static::insert(["table" => self::table, "field" => $field]);
            } else {
                $json['warn'] = static::update([
                    "table" => self::table,
                    "field" => $field,
                    "where" => $where
                ]);
            }
            if (in_array($json['warn'], ["新增成功", "更新成功"])) {
                delete(array('table'=>'meal_record_detail','where'=>" orderId = '$id'"));
                if (is_array($staffIdArr)) {
                    foreach ($staffIdArr as $stid) {
                        insert(array(
                            'table' => 'meal_record_detail',
                            'field' => array(
                                'id' => self::getId(),
                                'orderId' => $id,
                                'stid' => $stid,
                                'time' => $this->time
                            )
                        ));
                    }
                }
                //添加日志
                $type = mb_substr($json['warn'], 0, 2, 'utf-8');
                $text = $this->control['name'] . $type . "了" . self::title . " id：【" . $id . '】';
                $this->logText(["type" => self::page, "text" => $text]);
                //返回
                $_SESSION['warn'] = $json['warn'];
                $json['warn'] = 2;
                $json['href'] = root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=detail&id=" . $id;
            }
        }

        resp:
        return json_encode($json, 320);
    }

    /*详情页*/
    public function detail(): string
    {
        $info = query(self::table, " id = '{$this->get('id')}'");
        if (empty($info['id'])) {
            $html = '未找到此' . self::title;
        } else {
            //菜单
            $data = array(
                "id" => $info['id'],
                "title" => self::title . '详情',
            );

            $button = [];
            if ($this->power(self::page, "edit")) {
                //按钮
                $button[] = [
                    "name" => "编辑",
                    "type" => "弹出层",
                    "cla" => $this->get('cla'),
                    "fun" => "edit",
                    "post" => ["id" => $info['id']],
                    'style' => 'li-btn-blue-bg',
                ];
                //按钮
                $button[] = [
                    "name" => "删除",
                    "type" => "弹出层",
                    "cla" => $this->get('cla'),
                    "fun" => "operate",
                    "post" => ["act" => 'del', "id" => $info['id']],
                ];

            }

//            $member = find(array('table'=>self::table.'_detail','single'=>" orderId = '$info[id]' group by orderId",'field'=>" group_concat(any_value(stid)) as member"));
//            $obj = new performanceScoreClass();
            //生成表单
            $data['html'] = $this->tableShow(array(
                "button" => $button,
                '基本信息' => array(
                    array(
                        "日期" => $info['recordDate'],
                        "食堂名称" => $info['canteen'],
                        "餐别" => $info['type'],
                        "访客就餐人数" => $info['visitorNum'],
                    ),
                    array(
                        "员工就餐人员" =>componentClass::showSelector($info['selector']),
                    ),
                    array(
                        "备注" => pre($info['text']),
                    ),
                ),
                '系统信息' => array(
                    array(
                        "单据编号" => $info['orderNo'],
                        "创建人" => self::findStaff($info['stid']),
                        "创建时间" => $info['time'],
                        "更新时间" => $info['updateTime'],
                    ),
                )
            ));
            //生成页面
            $html = $this->tab($data);
        }
        //返回
        return $html;
    }


    // 操作中转
    public function operate()
    {
        $act = $this->post('act') ?? '';
        $pwd = false; // 需要密码
        $form = $power = '';
        switch ($act) {
            default:
                $act = 'del';
                $msg = '删除' . self::title;
                $title = self::page. 'Delete';
                $function = 'delete';
                $form = self::page . 'OperateForm, listForm';
                $power = '';
                break;
        }
        if (!$this->power(self::page, $act)) {
            $_SESSION['warn'] = '你还没有' . $msg . '的权限';
            $json = [
                'warn' => 2,
                'href' => root . 'control/adIndex.html?cla=' . $this->get('cla') . '&fun=home',
            ];
        } else {
            return $this->confirm([
                'title' => $title,
                'function' => $function,
                'form' => $form,
                'power' => $power,
                'pwd' => $pwd,
            ]);
        }

        resp:
        return json_encode($json, 320);
    }

    /**
     * 删除
     * @return bool|string
     */
    public function delete()
    {
        $array = $this->post('listId') ?? $this->post('id');
        if (!$this->power(self::page, "del")) {
            $json['warn'] = "你没有删除的权限";
        } elseif (empty($array)) {
            $json['warn'] = "你一个数据都没有选择呢";
        } else {
            $text = "";
            $x = 0;
            $array = is_array($array) ? $array : [$array];
            foreach ($array as $id) {
                $info = query(self::table, " id = '$id' ");
                    //最后删除基本参数
                    delete(array("table" => self::table, "where" => " id = '$id' "));
                    //返回
                    $warn = "删除成功";
                    $x++;
                $text .= self::title . "ID【" . $info['id'] . "】" . $warn . "。<br>";
            }
            //添加日志
            $text = $this->control['name'] . "删除了" . self::title . "，结果如下：<br>" . $text;
            $this->logText(array("type" => self::page, "text" => $text));
            $_SESSION['warn'] = "删除了" . $x . "个" . self::title . "，详情见系统日志。";
            $json['warn'] = 2;
            $json['href'] = root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=home";
        }
        return json_encode($json, 320);
    }
}

